package com.zeny.edu.eduservice.controller;


import com.zeny.edu.common.ResponseResult;
import com.zeny.edu.eduservice.entity.EduChapter;
import com.zeny.edu.eduservice.entity.dto.EduChapterDto;
import com.zeny.edu.eduservice.service.EduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author zeny
 * @since 2020-03-05
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    /**
     * @param courseId
     * @return com.zeny.edu.common.ResponseResult
     * @Description 根据课程ID查询章节和小节
     * @Date 2020/3/5 0005 14:26
     **/
    @GetMapping("/getChapterVideoList/{courseId}")
    public ResponseResult getChapterVideoList(@PathVariable String courseId) {
        List<EduChapterDto> list = eduChapterService.getChapterVideoListByCourseId(courseId);
        return ResponseResult.ok().data("items", list);
    }

    /**
     * @param eduChapter
     * @return com.zeny.edu.common.ResponseResult
     * @Description 添加章节
     * @Date 2020/3/5 0005 18:40
     **/
    @PostMapping("/addChapter")
    public ResponseResult addChapter(@RequestBody EduChapter eduChapter) {
        boolean success = eduChapterService.save(eduChapter);
        return success ? ResponseResult.ok() : ResponseResult.fail();
    }

    /**
     * @Description 根据章节ID查询章节
     * @Date 2020/3/5 0005 18:44
     * @param chapterId
     * @return com.zeny.edu.common.ResponseResult
     **/
    @GetMapping("/getChapterInfo/{chapterId}")
    public ResponseResult getChapterInfo(@PathVariable String chapterId) {
        EduChapter eduChapter = eduChapterService.getById(chapterId);
        return ResponseResult.ok().data("chapter", eduChapter);
    }


    /**
     * @Description 修改章节
     * @Date 2020/3/5 0005 19:17
     * @param eduChapter
     * @return com.zeny.edu.common.ResponseResult
     **/
    @PutMapping("/updateChapter")
    public ResponseResult updateChapter(@RequestBody EduChapter eduChapter) {
        boolean success = eduChapterService.updateById(eduChapter);
        return success ?  ResponseResult.ok() : ResponseResult.fail();
    }

    /**
     * @Description 删除章节
     * @Date 2020/3/5 0005 19:20
     * @param chapterId
     * @return com.zeny.edu.common.ResponseResult
     **/
    @DeleteMapping("/deleteChapter/{chapterId}")
    public ResponseResult deleteChapterById(@PathVariable String chapterId) {
        boolean success = eduChapterService.removeChapterById(chapterId);
        return success ? ResponseResult.ok() : ResponseResult.fail();
    }

}

